const axio = new Object({
    get: function (url) {
        return new Promise(function (resolve, reject) {
            let request = new XMLHttpRequest();
            request.open("GET", url);
            request.onload = () => {
                request.status == 200
                    ? resolve(request.response)
                    : reject(Error(request.statusText));
            };
            request.onerror = () => {
                reject(Error("Network Error"));
            };
            request.send();
        });
    },
    post: function (url, data, that = null, index = 0) {
        return new Promise(function (resolve, reject) {
            let request = new XMLHttpRequest();
            if (that) {
                request.upload.onprogress = (event) => {
                    if (event.lengthComputable) {
                        let complete = ((event.loaded / event.total) * 100) | 0;
                        that.handleUpdateProcess(index, complete);
                    }
                };
            }
            request.open("POST", url);
            request.onload = () => {
                request.status == 200
                    ? resolve(request.response)
                    : alert(request.response);
                reject(Error(request.statusText));
            };
            request.onerror = () => {
                console.log(request.response);
                reject(Error("Network Error"));
            };
            request.send(data);
        });
    },
    stream: function (method = "get", url, filename = "downloaded", data = null) {
        return new Promise(function (resolve, reject) {
            let request = new XMLHttpRequest();
            request.open(method, url);
            request.responseType = "blob";

            request.onload = () => {
                if (request.status == 200) {
                    const downloadURL = window.URL.createObjectURL(request.response);
                    const a = document.createElement("a");
                    // 下载后文件的名字
                    a.download = filename;
                    a.href = downloadURL;
                    document.body.appendChild(a);
                    a.click();
                    setTimeout(function () {
                        // 移除内存中的临时文件路径和为下载而创建的a标签
                        URL.revokeObjectURL(downloadURL);
                        a.remove();
                    }, 10000);
                    resolve(request.response);
                } else {
                    //完成未完成，返回错误
                    //由于前面设置了responseType=blob，所以需要手动utf-8解码才能获取服务器返回的错误信息文本
                    let resp = "";
                    if (request.status < 500) {
                        const fr = new FileReader();
                        fr.readAsText(request.response);
                        fr.onload = () => {
                            fr.result
                                ? (resp = "[" + request.status + "]" + fr.result.toString())
                                : (resp = "[" + request.status + "]" + "服务器未返回数据");
                        };
                    } else {
                        resp = "服务器错误";
                    }
                    console.log(resp);
                    //alert(resp)
                    reject(Error(request.statusText));
                }
            };
            request.onerror = function () {
                //无实际意义
                reject(Error("Network Error"));
            };
            request.send(data);
        });
    },
});
export default axio
